#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

struct node {
	int num[4], step;
} first, final;
int vis[10][10][10][10];

void bfs() {
	node now, next;
	queue<node> q;
	memset(vis, 0, sizeof vis);
	now = first;
	now.step = 0;
	vis[now.num[0]][now.num[1]][now.num[2]][now.num[3]] = 1; 
	q.push(now);
	while (!q.empty()) {
		now = q.front();
		q.pop();
		if (now.num[0] == first.num[1] && now.num[1] == first.num[1] && now.num[2] == now.num[2]) {
			cout << "step = " << now.step << endl;
			return;
		}
		
		for (int i = 0; i < 4; i++) {
			next = now;
			next.num[i]++;
			if (next.num[i] == 10) {
				next.num[i] = 1;
			}
			if (!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) {
				vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1;
				next.step++;
				q.push(next);
			}
 		}
		 
		 for (int i = 0; i < 4; i++) {
			next = now;
			next.num[i]--;
			if (next.num[i] == 0) {
				next.num[i] = 9;
			}
			if (!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) {
				vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1;
				next.step++;
				q.push(next);
			}
 		} 		
 		
 		
 		for (int i = 0; i < 3; i++) {
			next = now;
			next.num[i] = now.num[i+1];
			next.num[i+1] = now.num[i];
			if (!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) {
				vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]] = 1;
				next.step++;
				q.push(next);
			}
 		}
 		
	}
	
}

int main(void) {
	char start[4], tail[4];
	cin >> start;
	cin >> tail;
	for (int i = 0; i < 4; i++) {
		first.num[i] = start[i] - '0';
		final.num[i] = tail[i] - '0';
	}
	bfs();
	return 0;
}
